heroes.Powers = heroes.Powers or
{
	
	{
		
		Name = "none",
		Desc = "Remove superpowers",
		Id = -1
		
	}
	
};

heroes.Installed = false;

heroes.MenuOpen = false;
heroes.MenuWidth = 256;
heroes.MenuSpeed = 15;
heroes.MenuPos = Vector( -heroes.MenuWidth, 0, 0 ); -- to make it not flicker at startup

heroes.SelectedPower = 1;
heroes.CurrentPower = -1;

heroes.NextKeyCheck = 0;
heroes.KeyCheckDelay = 0.2; -- don't change this! this is a specific value that apparently works best

local grad = surface.GetTextureID( "vgui/gradient-r" );

surface.CreateFont( "coolvetica", 20, 500, true, false, "MenuFont1" );

surface.CreateFont( "coolvetica", 18, 500, true, false, "MenuFont2" );
surface.CreateFont( "coolvetica", 16, 500, true, false, "MenuFont3" );

local text = "TEXT";

surface.SetFont( "MenuFont2" );
local _, h = surface.GetTextSize( text );

surface.SetFont( "MenuFont3" );
local _, h2 = surface.GetTextSize( text );

//calc item height using font size
heroes.MenuItemTall = 5 + h + 5 + h2 + 5;

heroes.Skin =
{
	
	GradientStart = Color( 145, 145, 145, 255 ),
	GradientEndCl = Color( 185, 185, 185, 255 ),
	SideLineColor = Color( 050, 150, 255, 250 ),
	ListBorderCol = Color( 050, 150, 255, 250 ),
	ListItemColor = Color( 145, 145, 145, 255 ),
	ListItemCheck = Color( 185, 185, 185, 255 ),
	ListItemSideL = Color( 050, 150, 255, 250 ),
	MenuTextColor = Color( 255, 255, 255, 255 )
	
};

local function clr( col )
	
	return col.r, col.g, col.b, col.a;
	
end

local function inv( col )
	
	return Color( 255 - col.r, 255 - col.g, 255 - col.b, col.a );
	
end

function heroes.SetupHeroes( msg )
	
	if msg:ReadBool( ) then
		
		heroes.Installed = true;
		
	end
	
end
usermessage.Hook( "SetupHeroes", heroes.SetupHeroes );

function heroes.GetPower( name )
	
	for i, power in pairs( heroes.Powers ) do
		
		if power.Id == name || power.Name == name then
			
			return power, i;
			
		end
		
	end
	
end

function heroes.SendPower( power )
	
	RunConsoleCommand( "heroes_set_power", power );
	
end

function heroes.SetPower( msg )
	
	if !heroes.Installed then return; end
	
	local pl = msg:ReadEntity( );
	local id = msg:ReadShort( );
	
	if pl && id then
		
		pl.PowerId = id;
		
		local power, i = heroes.GetPower( id );
		
		if power then
			
			heroes.SelectedPower = i;
			heroes.CurrentPower = i;
			
		end
		
		//something for avalon
		if avaux && avalon && avalon:IsLibraryLoaded( "mount" ) then
			
			local mount = avalon:GetMount( "Label" );
			
			if mount then
				
				local power = heroes.GetPower( id );
				
				if power then
					
					mount:SetLabelLine( pl, "Power", power.Name );
					
				else
					
					mount:SetLabelLine( pl, "Power", nil );
					
				end
				
			end
			
		end
		
	end
	
end
usermessage.Hook( "SetPwr", heroes.SetPower );

function heroes.AddPower( msg )
	
	if !heroes.Installed then return; end
	
	local power = { };
	power.Name = msg:ReadString( );
	power.Desc = msg:ReadString( );
	power.Id = msg:ReadShort( );
	
	table.insert( heroes.Powers, power );
	
end
usermessage.Hook( "AddPwr", heroes.AddPower );

//skin this stuff
function heroes.HUDPaint( )
	
	if !heroes.Installed then return; end
	
	if #heroes.Powers > 1 then
		
		local x, y, w, h = heroes.MenuPos.x, heroes.MenuPos.y, heroes.MenuWidth, 256;
		w = w - 4;
		
		surface.SetFont( "MenuFont1" );
		local text = "Select your power :";
		local tw, th = surface.GetTextSize( text );
		
		h = 5 + th + 5 + ( #heroes.Powers * heroes.MenuItemTall + ( #heroes.Powers - 1 ) * 5 ) + 5 + 1;
		
		surface.SetDrawColor( clr( heroes.Skin.SideLineColor ) );
		surface.DrawRect( x + w, y, 4, h );
		
		surface.SetDrawColor( clr( heroes.Skin.GradientEndCl ) );
		surface.DrawRect( x, y, w, h );
		
		surface.SetTexture( grad );
		surface.SetDrawColor( clr( heroes.Skin.GradientStart ) );
		surface.DrawTexturedRectRotated( x + w / 2, y + h / 2, w, h, 180 );
		
		surface.SetTextColor( clr( inv( heroes.Skin.MenuTextColor ) ) );
		surface.SetTextPos( x + 5 + 2, y + 5 + 2 );
		surface.DrawText( text );
		
		surface.SetTextColor( clr( heroes.Skin.MenuTextColor ) );
		surface.SetTextPos( x + 5, y + 5 );
		surface.DrawText( text );
		
		surface.SetDrawColor( clr( heroes.Skin.ListBorderCol ) );
		surface.DrawLine( x + 5, y + 5 + th + 5, x + w - 5, y + 5 + th + 5 );
		surface.DrawLine( x + 5, y + h - 5, x + w - 5, y + h - 5 );
		surface.DrawLine( x + 5, y + 5 + th + 5, x + 5, y + h - 5 );
		surface.DrawLine( x + w - 5, y + 5 + th + 5, x + w - 5, y + h - 5 );
		
		local ix, iy = x + 5 + 1, y + 5 + th + 5 + 1;
		local iw = w - 5 - 5 - 1;
		local iiy = 0;
		
		local c = 0;
		
		for i, power in pairs( heroes.Powers ) do
			
			//hm..
			if i == 1 then
				
				iiy = iy;
				
			else
				
				iiy = iy + 5;
				
			end
			
			local offset = c * heroes.MenuItemTall + ( math.Clamp( c - 1, 0, #heroes.Powers ) ) * 5;
			
			heroes.DrawButton( ix, iiy + offset, iw, heroes.MenuItemTall, i, i == heroes.SelectedPower );
			
			c = c + 1;
			
		end
		
	end
	
end
hook.Add( "HUDPaint", "heroes.HUDPaint", heroes.HUDPaint );

function heroes.DrawButton( x, y, w, h, power, selected )
	
	power = heroes.Powers[ power ];
	
	if power then
		
		surface.SetDrawColor( clr( ( selected ) && heroes.Skin.ListItemCheck || heroes.Skin.ListItemColor ) );
		surface.DrawRect( x, y, w, h );
		
		surface.SetDrawColor( clr( heroes.Skin.ListItemSideL ) );
		surface.DrawRect( x, y, 4, h );
		
		surface.SetFont( "MenuFont2" );
		
		surface.SetTextColor( 0, 0, 0, 255 );
		surface.SetTextPos( x + 4 + 5 + 2, y + 5 + 2 );
		surface.DrawText( power.Name );
		
		surface.SetTextColor( clr( heroes.Skin.MenuTextColor ) );
		surface.SetTextPos( x + 4 + 5, y + 5 );
		surface.DrawText( power.Name );
		
		surface.SetFont( "MenuFont3" );
		
		local _, h2 = surface.GetTextSize( power.Desc );
		
		surface.SetTextColor( 0, 0, 0, 255 );
		surface.SetTextPos( x + 4 + 5 + 2, y + h - 5 - h2 + 2 );
		surface.DrawText( power.Desc );
		
		surface.SetTextColor( clr( heroes.Skin.MenuTextColor ) );
		surface.SetTextPos( x + 4 + 5, y + h - 5 - h2 );
		surface.DrawText( power.Desc );
		
	end
	
end

function heroes.Think( )
	
	if !heroes.Installed then return; end
	
	if #heroes.Powers > 1 then
		
		heroes.MenuPos.y = ScrH( ) * 0.2;
		
		local val = heroes.MenuPos.x;
		
		if heroes.MenuOpen then
			
			val = val + heroes.MenuSpeed;
			
		else
			
			val = val - heroes.MenuSpeed;
			
		end
		
		val = math.Clamp( val, -heroes.MenuWidth, 0 );
		
		heroes.MenuPos.x = val;
		
		if CurTime( ) > heroes.NextKeyCheck then
			
			if input.IsKeyDown( KEY_UP ) then
				
				heroes.GoToPrev( );
				heroes.NextKeyCheck = CurTime( ) + heroes.KeyCheckDelay;
				
			elseif input.IsKeyDown( KEY_DOWN ) then
				
				heroes.GoToNext( );
				heroes.NextKeyCheck = CurTime( ) + heroes.KeyCheckDelay;
				
			end
			
		end
		
		//doesn't seem to work here.. need work on a more controled system later
		if input.WasKeyTyped( KEY_UP ) || input.WasKeyTyped( KEY_DOWN ) then
			
			heroes.NextKeyCheck = CurTime( ) - heroes.KeyCheckDelay;
			
		end
		
	end
	
end
hook.Add( "Think", "heroes.Think", heroes.Think );

function heroes.GoToNext( )
	
	heroes.SelectedPower = heroes.SelectedPower + 1;
	
	if heroes.SelectedPower > #heroes.Powers then
		
		heroes.SelectedPower = 1;
		
	end
	
end

function heroes.GoToPrev( )
	
	heroes.SelectedPower = heroes.SelectedPower - 1;
	
	if heroes.SelectedPower <= 0 then
		
		heroes.SelectedPower = #heroes.Powers;
		
	end
	
end

function heroes.CC_OpenMenu( _, cmd )
	
	if !heroes.Installed then return; end
	
	heroes.MenuOpen = ( cmd:sub( 1, 1 ) == "+" ) && true || false;
	
	if !heroes.MenuOpen && heroes.CurrentPower != heroes.SelectedPower then
		
		heroes.SendPower( heroes.Powers[ heroes.SelectedPower ].Id );
		
		heroes.CurrentPower = heroes.SelectedPower;
		
	end
	
end
concommand.Add( "+heroes_menu", heroes.CC_OpenMenu );
concommand.Add( "-heroes_menu", heroes.CC_OpenMenu );